﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls.ContentControls.FrontEnd;
using System.Text;
using System.Web.UI.HtmlControls;

public partial class Templates_newUsers_Default : LoggedInPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

        var data = Acms.Bll.User.GetMostRecent(this.AcmsContext.SiteId, 10);
        rptNewUsers.DataSource = data;
        rptNewUsers.DataBind();

        if (!IsPostBack)
        {
            DataBind();
        }
    }

    private CarbonExchange.Bll.Country[] _Countries;
    protected CarbonExchange.Bll.Country[] Countries
    {
        get
        {
            if (_Countries == null)
            {
                CarbonExchange.Bll.CountryFactory factory = new CarbonExchange.Bll.CountryFactory();
                _Countries = factory.GetAllBySiteId(AcmsContext.SiteId).ToArray();
            }
            return _Countries;
        }
    }

    protected void rptNewUsers_DataBound(object sender, RepeaterItemEventArgs e)
    {
        Acms.Bll.User user = e.Item.DataItem as Acms.Bll.User;
        if (user != null)
        {
            Image imgNoThumb = (Image)e.Item.FindControl("imgNoThumb");
            AcmsImage imgThumb = (AcmsImage)e.Item.FindControl("imgThumb");
            //set fileif to thumbnail id and then hide or show if set
            imgThumb.FileId = user.GetThumbnailId();
            imgThumb.Visible = imgThumb.FileId.HasValue;
            imgNoThumb.Visible = !imgThumb.Visible;

            //build up a region string
            Label lblRegion = (Label)e.Item.FindControl("lblRegion");
            StringBuilder tmpRegion = new StringBuilder();
            tmpRegion.Append(user.GetDepartment());
            if (tmpRegion.Length > 0)
            {
                tmpRegion.Append(", ");
            }
            if (!string.IsNullOrEmpty(user.GetState()))
            {
                tmpRegion.Append(user.GetState());
                tmpRegion.Append(", ");
            }
            tmpRegion.Append( Countries.Where(x => x.Id == user.GetCountryId()).Select(x => x.Name).SingleOrDefault());
            lblRegion.Text = tmpRegion.ToString();

            //footprint savings
            Label lblFootprint = (Label)e.Item.FindControl("lblFootprint");
            //Label lblFootprintCost = (Label)e.Item.FindControl("lblFootprintCost");
            Label lblPredictedSaving = (Label)e.Item.FindControl("lblPredictedSaving");
            //Label lblPredictedSavingCost = (Label)e.Item.FindControl("lblPredictedSavingCost");

            var footprintSavingFactory = new CarbonExchange.Bll.FootprintSavingFactory();
            var footprintSaving = footprintSavingFactory.GetByUserId(user.Id);
            if (footprintSaving != null)
            {
                lblFootprint.Text = footprintSaving.LatestFootprint.ToString("#,##0  kg/CO<sub>2</sub>e");
                
                //lblFootprintCost.Text = ConvertToCurrencyString(footprintSaving.LatestFootprint);
                if (footprintSaving.PotentialSaving.HasValue)
                {
                    lblPredictedSaving.Text = footprintSaving.PotentialSaving.Value.ToString("#,##0  kg/CO<sub>2</sub>e");
                    //lblPredictedSavingCost.Text = ConvertToCurrencyString(footprintSaving.PotentialSaving.Value);
                }
            }

            var pComment = (HtmlGenericControl)e.Item.FindControl("pComment");
            pComment.InnerText = user.GetComment();
        }
    }

}
